package com.socodethis.spaceex;

import java.util.ArrayList;
import java.util.Random;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.socodethis.spaceex.math.cPoint;

public class attackHolder {
	private ArrayList<attackClass> _attacks = new ArrayList<attackClass>();
	Random random = new Random(System.nanoTime());
	Sprite sprite;
	private int attackSpeed, attackCounter,i;
	private float cAttack;
	private cPoint temp;
	AttackEnemy holderE;
	AttackPlayer holderP;
	Sprite tempSprite;
	float angle;
	texturePackerLoader tAttacks = new texturePackerLoader("data/player_attacks.txt","data/player_attacks.png",0);
	public attackHolder(cPoint size){
		temp = new cPoint(size);
		temp.scalePoint((float) 0.4);
		_attacks = new ArrayList<attackClass>();
		//Here to count the cycles
		attackCounter = 0;
		cAttack = 1;
	}
	public void clear(){
		_attacks.clear();
	}
	public void addCycle(){
		attackCounter++;
		if(cAttack==3){
			if(attackCounter>attackSpeed){
				attackCounter=0;
			}
		}
	}
	public boolean getCycles(){
		//If the player is charged for another attack..
		if(cAttack!=3){
			if(attackCounter>=attackSpeed){
				attackCounter=0;
				return true;
			}
		}else{
			if(attackCounter==attackSpeed){
				return true;
			}
		}
		return false;
	}
	public void addAttack(cPoint start, cPoint end, enemyClass enemy, AttackLoader currentAttack){
		holderE = new AttackEnemy(start,end,currentAttack.getAMovementSpeed(), currentAttack.getARadius(), enemy, currentAttack.getAMovePattern());
		cAttack = currentAttack.getAMovePattern();
		this.attackSpeed = (int) currentAttack.getASpeed();
		sprite = new Sprite(tAttacks.getName(currentAttack.getName()));
		sprite.setSize(temp.getX(), temp.getY());
		_attacks.add(holderE);
	}
	
	public void addAttack(cPoint start, cPoint end, playerClass player, AttackLoader currentAttack){
		holderP = new AttackPlayer(start,end,currentAttack.getAMovementSpeed(), currentAttack.getARadius(), player, currentAttack.getAMovePattern());
		cAttack = currentAttack.getAMovePattern();
		this.attackSpeed = (int) currentAttack.getASpeed();
		sprite = new Sprite(tAttacks.getName(currentAttack.getName()));
		sprite.setSize(temp.getX(), temp.getY());
		_attacks.add(holderP);
	}
	
	public void incrementAttacks(){
		for(i = 0; i < _attacks.size(); i++){
			switch((int)_attacks.get(i).getAttackP()){
				case 2:
					_attacks.get(i).setSpeed(new cPoint((float)(_attacks.get(i).getSpeed().getX() + (random.nextInt( 3 )-1)*.025),
							(float)(_attacks.get(i).getSpeed().getY() + (random.nextInt( 3 )-1)*.025)));break;
			}
			//Cast the attack to the movement position of choice
			if(_attacks.get(i) instanceof AttackEnemy){
				((AttackEnemy) _attacks.get(i)).moveAttack();
			}
			if(_attacks.get(i) instanceof AttackPlayer){
				((AttackPlayer) _attacks.get(i)).moveAttack();
			}
			if(_attacks.get(i).finished){
				_attacks.remove(i);
				i--;
			}
		}
	}
	public Sprite returnSprite(int index, Color color) {
		tempSprite = new Sprite(sprite);
		tempSprite.setPosition((float) (_attacks.get(index).getCurrent().getX()), _attacks.get(index).getCurrent().getY());
		tempSprite.setOrigin(0, 0);
		tempSprite.setRotation(getAngle(_attacks.get(index)));
		tempSprite.setColor(color);
		return tempSprite;
	}
	public int getSize(){
		return _attacks.size();
	}
	public float getAngle(attackClass attack) {
	    angle = (float) Math.toDegrees(Math.atan2(attack.getEnd().getX() - attack.getCurrent().getX(), attack.getEnd().getY() - attack.getCurrent().getY()));
	    return angle<0?angle+=360:angle;
	    //return angle;
	}
}
